/** 
 * ConnectionFactory.java
 * 
 * 1.0
 * 
 * 04/07/2014
 * 
 * Copyright (c) 2013 FPT Software.
 * FPT Building, Street No1, DaNang Industrial Zone, AnDon, 
 * AnHaiBac Ward, SonTra District, DaNang City.
 * All rights reserved.
 */
package com.jms.fu.model.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.jms.fu.common.Common;
import com.jms.fu.common.UIMessage;
import com.jms.fu.exception.DAOException;



/**
 * Class to get connection form database
 * 
 * @author Design Framework Group
 */
public class ConnectionFactory {
	
	/**
	 * Method to get Connection
	 * @return an connection variable
	 * @throws DAOException
	 */
    public static Connection getConnection() throws DAOException {
        try {
            // Get driver class
            Class.forName(Common.getProperty("driver"));

            // Get login info from Common file
            String server = Common.getProperty("server");
            String port = Common.getProperty("port");
            String databaseName = Common.getProperty("databaseName");
            String userName = Common.getProperty("userName");
            String password = Common.getProperty("password");
            String url = "jdbc:sqlserver://" + server + ":" + port
                    + ";databaseName=" + databaseName; // Create url

            // Return connection
            return DriverManager.getConnection(url, userName,
                    password);
        } catch (ClassNotFoundException e) {
        	e.printStackTrace();
            throw new DAOException(UIMessage.DATABASE_CONNECTION_ERROR);
        } catch (SQLException e) {
        	e.printStackTrace();
            throw new DAOException(UIMessage.DATABASE_CONNECTION_ERROR);
        } catch (IOException e) {
        	e.printStackTrace();
            throw new DAOException(UIMessage.DATABASE_CONNECTION_ERROR);
        }
    }
}
